package cn.kgc.spring.convertors;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 课工场
 * @date 2024/11/5
 * @description  自定义日期转换器  String    Date
 *   Converter<S,T> 接口
 *     <S> – the source type
 *     <T> – the target type
 */
public class CustomerDateConverter implements Converter<String, Date> {

    private String pattern;


    public void setPattern(String pattern){
        this.pattern = pattern;
    }

    // "2024-11-5"
    @Override
    public Date convert(String source) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            Date parse = simpleDateFormat.parse(source);
            return  parse;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
